package com.fancy.mobileschool.service;


import com.fancy.mobileschool.entity.Course;
import com.fancy.mobileschool.entity.YunFile;
import com.fancy.mobileschool.util.OKHttpUtil;


import java.io.File;

import java.util.List;
import okhttp3.OkHttpClient;

/**
 * Created by Jackie on 2017/11/12.
 *
 */

public class YunFileService {

    private OkHttpClient httpClient = OKHttpUtil.getHttpClient();

    private static final String baseUrl = URLConfig.baseUrl +  "yun_file";

    private static final String baseUrlForPerson = baseUrl + "/person/";

    private static final String baseUrlForCourse = baseUrl + "/course/";

    public List<YunFile> getMyYunFiles(String myID) {
        List<YunFile> rs;
        String url = baseUrl + "/getMyFile.do?myID=" + myID;
        rs = OKHttpBase.fromJsonListByGet(url, YunFile.class);

        return rs;
    }

    public List<Course> getShareCourse() {
        List<Course> rs ;
        String url = baseUrlForCourse + "getShareCourse.do";
        rs = OKHttpBase.fromJsonListByGet(url, Course.class);
        return rs;
    }

    public List<Course> getMyCourses() {
        return getShareCourse();
    }

    public List<YunFile> getCourseShareFiles(String course_name) {
        List<YunFile> rs;
        String url = baseUrlForCourse + "getCourseShareFiles.do?course_name=" + course_name;
        rs = OKHttpBase.fromJsonListByGet(url, YunFile.class);
        return rs;
    }

    public boolean uploadPersonFile(File file, String myID) {
        String url = baseUrlForPerson + "uploadFile.do?myID=" + myID;
        return OKHttpBase.uploadFile(url, file);
    }

    public boolean uploadCourseFile(File file, String course_id, String myID) {
        String url = baseUrlForCourse + "uploadFile.do?course_name=" + course_id + "&myID=" + myID;
        return OKHttpBase.uploadFile(url, file);
    }

//    private boolean uploadFile(String url, File file) {
//        RequestBody filebody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
//        RequestBody body = new MultipartBody.Builder()
//                .addFormDataPart("file", file.getName(), filebody)
//                .build();
//        final Request request = new Request.Builder()
//                .url(url)
//                .post(body)
//                .build();
//
//        Response response = null;
//        try {
//            response = httpClient.newCall(request).execute();
//        } catch (IOException e) {
//
//        }
//
//        return response.code() == 200;
//    }
//
    //如果下载成功返回文件名
    public String downLoadPersonFile(String myID, String fileName, String desFileLocation) {

        return null;
    }

}
